package controllers

import (
	"encoding/json"
	"fmt"

	beego "github.com/beego/beego/v2/server/web"
)

type ApiLoginController struct {
	beego.Controller
}

type loginJson struct {
	FormData formData `json:"formData"`
	Meta     metaJson `json:"meta"`
	Token    string   `json:"token"`
}

type metaJson struct {
	Status  int    `json:"status"`
	Message string `json:"message"`
}

type formData struct {
	Username []string `json:"username"`
	Password []string `json:"password"`
}

func (c *ApiLoginController) Post() {
	var dataForm formData
	data := c.Ctx.Input.RequestBody
	fmt.Println(data)
	json.Unmarshal(data, &dataForm)
	// c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	fmt.Println(dataForm)
	c.Data["json"] = &loginJson{FormData: dataForm, Meta: metaJson{Status: 200, Message: "success"}, Token: "VG9rZW4K"}
	c.ServeJSON()
}

//json里面套数组的方案
// type loginJson struct {
// 	FormData formData `json:"formData"`
// 	Meta     metaJson `json:"meta"`
// }

// type metaJson struct {
// 	Status  int    `json:"status"`
// 	Message string `json:"message"`
// }

// type formData struct {
// 	Username []string `json:"username"`
// 	Password []string `json:"password"`
// }

// func (c *ApiLoginController) Post() {
// 	var dataForm formData
// 	data := c.Ctx.Input.RequestBody
// 	fmt.Println(data)
// 	json.Unmarshal(data, &dataForm)
// 	// c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
// 	fmt.Println(dataForm)
// 	c.Data["json"] = &loginJson{FormData: dataForm, Meta: metaJson{Status: 200, Message: "success"}}
// 	c.ServeJSON()
// }
